/*
 * This file is part of "IdeWiki" project.
 *
 * Copyright (c) 2008 Hendrik Ebel
 * All Rights Reserved.
 *
 * Please visit http://code.google.com/p/idewiki for updates and contact.
 *
 * --LICENSE NOTICE--
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 3
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; If not, see <http://www.gnu.org/licenses/>.
 * --LICENSE NOTICE--
 */
package ws.ebel.idewiki.preferences;

import org.eclipse.jface.preference.PreferencePage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.StyledText;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.FontData;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.FontDialog;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchPreferencePage;

import ws.ebel.idewiki.Activator;

/**
 * The Class IdeWikiPreferences.
 */
public class IdeWikiPreferences extends PreferencePage implements IWorkbenchPreferencePage {

    /** The wiki url. */
    private Text wikiUrl;
    
    /** The user name. */
    private Text userName;
    
    /** The user password. */
    private Text userPassword;
    
    /** the editor font */
    private FontData editorFont;
    
    /** the web cvs button */
    private Button webCvsEnableButton;
    
    /** the auto login button */
    private Button autoLoginInButton;
    
    /**
     * Creates the contents.
     * 
     * @param parent the parent
     * 
     * @return the control
     * 
     * @see PreferencePage#createContents(Composite)
     */
    @Override
    protected Control createContents(Composite parent) {
        
        Composite fieldComposite = new Composite(parent, SWT.NULL);

        GridLayout fieldLayout = new GridLayout();
        fieldLayout.numColumns = 1;
        fieldComposite.setLayout(fieldLayout);
        GridData data = new GridData(GridData.FILL_HORIZONTAL);
        fieldComposite.setLayoutData(data);

        addWikiUserPref(fieldComposite);
        addFontPref(fieldComposite);
        addBooleanFlags(fieldComposite);
        fieldComposite.pack();

        return fieldComposite;
    }

    /**
     * @param aFieldComposite
     */
    private void addBooleanFlags(final Composite aFieldComposite) {
        Composite fontComposite = new Composite(aFieldComposite, SWT.NULL);
        GridLayout fontLayout = new GridLayout();
        fontComposite.setLayout(fontLayout);
        GridData data = new GridData(GridData.FILL_HORIZONTAL);
        fontComposite.setLayoutData(data);
        
        Group groupComposite = new Group(fontComposite, SWT.SHADOW_ETCHED_IN);
        GridLayout gridLayout = new GridLayout();
        gridLayout.numColumns = 1;
        groupComposite.setLayout(gridLayout);
        groupComposite.setText("Special Settings");

        webCvsEnableButton = new Button(groupComposite, SWT.CHECK);
        webCvsEnableButton.setText("Web CVS linking");
        webCvsEnableButton.setSelection(Activator.getDefault().getIsWebCvsEnabled());
        
        autoLoginInButton = new Button(groupComposite, SWT.CHECK);
        autoLoginInButton.setText("Auto Login");
        autoLoginInButton.setSelection(Activator.getDefault().getIsAutoLoginEnabled());
        
    }

    /**
     * @param aFieldComposite
     */
    private void addFontPref(final Composite aFieldComposite) {
        Composite fontComposite = new Composite(aFieldComposite, SWT.NULL);
        GridLayout fontLayout = new GridLayout();
        fontComposite.setLayout(fontLayout);
        GridData data = new GridData(GridData.FILL_HORIZONTAL);
        fontComposite.setLayoutData(data);

        Group groupComposite = new Group(fontComposite, SWT.SHADOW_ETCHED_IN);
        groupComposite.setLayout(new RowLayout());
        groupComposite.setText("Wiki Editor Font Setting");
        
        final StyledText text = new StyledText(groupComposite, SWT.WRAP | SWT.BORDER |SWT.READ_ONLY | SWT.MULTI | SWT.VERTICAL);
        text.setText("Example __WIKI__ syntax [content] \n with macros and {tm}other{tm} \n things. \n");
        Font prefFont = new Font(aFieldComposite.getDisplay(), Activator.getDefault().getWikiEditorFontPreference());
        text.setFont(prefFont);
        final Button button = new Button(groupComposite, SWT.PUSH);
        button.setText("Change Font");
        button.addListener(SWT.Selection, new Listener() {
            public void handleEvent(Event event) {
                FontDialog dialog = new FontDialog(aFieldComposite.getShell());
                dialog.setFontList(text.getFont().getFontData());
                dialog.open();
                
                Font selectedFont = new Font(aFieldComposite.getDisplay(), dialog.getFontList());
                text.setFont(selectedFont);
                if(selectedFont.getFontData() != null && selectedFont.getFontData().length > 0){
                    editorFont = selectedFont.getFontData()[0];
                }
                text.redraw();
            }           
        });
    }

    /**
     * @param fieldComposite
     */
    private void addWikiUserPref(Composite aFieldComposite) {
        Composite userComposite = new Composite(aFieldComposite, SWT.NULL);
        GridLayout userLayout = new GridLayout();
        userLayout.numColumns = 1;
        userComposite.setLayout(userLayout);
        GridData parentData = new GridData(GridData.FILL_HORIZONTAL);
        userComposite.setLayoutData(parentData);
        
        Group groupComposite = new Group(userComposite, SWT.SHADOW_ETCHED_IN);
        GridLayout gridLayout = new GridLayout();
        gridLayout.numColumns = 2;
        groupComposite.setLayout(gridLayout);
        groupComposite.setText("Wiki User Account Setting");
        
        GridData gridData = new GridData(GridData.FILL_HORIZONTAL);
        groupComposite.setLayoutData(gridData);
        
        Label rpcServer_label = new Label(groupComposite, SWT.NONE);
        rpcServer_label.setText("Wiki URL:");
        wikiUrl = new Text(groupComposite, SWT.BORDER);
        wikiUrl.setText(Activator.getDefault().getWikiUrlPreference());
        gridData = new GridData(GridData.FILL_HORIZONTAL);
        gridData.grabExcessHorizontalSpace = true;
        wikiUrl.setLayoutData(gridData);

        Label userNameLabel = new Label(groupComposite, SWT.NONE);
        userNameLabel.setText("User name:");
        userName = new Text(groupComposite, SWT.BORDER);
        userName.setText(Activator.getDefault().getUserNamePreference());
        gridData = new GridData(GridData.FILL_HORIZONTAL);
        gridData.grabExcessHorizontalSpace = true;
        userName.setLayoutData(gridData);

        Label passwordLabel = new Label(groupComposite, SWT.NONE);
        passwordLabel.setText("Password:");
        userPassword = new Text(groupComposite, SWT.BORDER | SWT.PASSWORD);
        userPassword.setText(Activator.getDefault().getUserPasswordPreference());
        gridData = new GridData(GridData.FILL_HORIZONTAL);
        gridData.grabExcessHorizontalSpace = true;
        userPassword.setLayoutData(gridData);
    }
    
    /**
     * Init.
     * 
     * @param workbench the workbench
     * 
     * @see org.eclipse.ui.IWorkbenchPreferencePage#init(org.eclipse.ui.IWorkbench)
     */
    public void init(IWorkbench workbench) {
        setPreferenceStore(Activator.getDefault().getPreferenceStore());
    }

    /**
     * Performs special processing when this page's Restore Defaults button has
     * been pressed. Sets the contents of the text field to be the default
     */
    @Override
    protected void performDefaults() {
        wikiUrl.setText(Activator.getDefault().getDefaultWikiUrlPreference());
        userName.setText(Activator.getDefault().getDefaultUserNamePreference());
        userPassword.setText(Activator.getDefault().getDefaultUserPaswordPreference());
        editorFont = Activator.getDefault().getDefaultWikiEditorFontPreference();
        webCvsEnableButton.setSelection(Activator.getDefault().getDefaultIsWebCvsEnabled());
        autoLoginInButton.setSelection(Activator.getDefault().getDefaultIsAutoLoginEnabled());
    }

    /**
     * Method declared on IPreferencePage. Save the expTask preference to the
     * preference store.
     * 
     * @return true, if perform ok
     */
    @Override
    public boolean performOk() {
        Activator.getDefault().setWikiUrlPreference(wikiUrl.getText());
        Activator.getDefault().setUserNamePreference(userName.getText());
        Activator.getDefault().setUserPasswordPreference(userPassword.getText());
        Activator.getDefault().setWikiEditorFontPreference(editorFont);
        Activator.getDefault().setIsWebCvsEnabled(webCvsEnableButton.getSelection());
        Activator.getDefault().setIsAutoLoginEnabled(autoLoginInButton.getSelection());
        return super.performOk();
    }

}
